package com.zsrt.day20170412.student.dao;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.Test;

import com.zsrt.day20170412.student.entity.StudentEntity;

/**
*@author jzh
*@创建时间 2017年4月12日上午10:21:18
*类说明：
*/
public class StudentDao {
	/**
	 * 查找不及格的学生，并打印出个人信息
	 * @param students 查找的范围数组
	 * @return         返回不及格对象的数组
	 */
	public StudentEntity[] findStudentScoerNG(StudentEntity[] students){
		int counter=0;
		for(int index=0;index<students.length;index++){
			StudentEntity student=new StudentEntity();
			student=students[index];
			double score=student.getScore();
			if(score<60){
				counter++;
			}
		}
		StudentEntity[] newStudents=new StudentEntity[counter];
		counter=0;
		for(int index=0;index<students.length;index++){
			StudentEntity student=new StudentEntity();
			student=students[index];
			double score=student.getScore();
			if(score<60){
				
				newStudents[counter]=student;
				counter++;
			}
		}
		return newStudents;
	}
	
	
	/**
	 * 更改一个学生的成绩
	 * @param students  要更改的学生对象数组
	 * @param name      待更改的学生名
	 * @param score     更改后的成绩
	 * @return          返回该学生对象
	 */
	public StudentEntity updateStudentScore(StudentEntity[] students,String name,double score){
		
		int index=studentExistOrNotByName(students, name);
		if(index>=0){
			students[index].setScore(score);
		}
		return students[index];
	}
	
	
	/**
	 * 判断输入的学生name是否存在
	 * @param students 要查找的学生对象数组
	 * @param name     待查找的学生名
	 * @return         存在，返回下标，不存在，返回-1
	 */
	public int studentExistOrNotByName(StudentEntity[] students,String name){
		int xb=-1;
		for(int index=0;index<students.length;index++){
			StudentEntity student = students[index];
			String newName=student.getName();
			if(newName.equals(name)){
				return index;
			}
		}
		return xb;
	}
	/**
	 * 通过名字删除单个学生
	 * @param students  要删除的学生对象数组
	 * @param name      待删除的学生名
	 * @return          返回删除后的数组
	 */
	public StudentEntity[] deleteStudentByName(StudentEntity[] students,String name){
		//newStudents用来保存删除后的数组
		StudentEntity[] newStudents=new StudentEntity[students.length-1];
		int index=studentExistOrNotByName(students, name);
		if(index>=0){
			for(int i=0;i<index;i++){
				newStudents[i]=students[i];
			}
			for(int i=index+1;i<students.length;i++){
				newStudents[i-1]=students[i];				
			}
		}
		return newStudents;	
	}
	/*public StudentEntity[] deleteSomeStudentByName(StudentEntity[] students,String[] names){
		//newStudents用来保存删除后的数组
		StudentEntity[] newStudents;
		for(int i=0;i<names.length;i++){
			newStudents=new StudentEntity[students.length-i-1];
			String name=names[i];
			int index=studentExistOrNotByName(students, name);
			if(index>=0){
				for(int j=0;j<index;j++){
					newStudents[j]=students[j];
				}
				for(int j=index+1;j<students.length;j++){
					newStudents[j-1]=students[j];				
				}
			}
		}
		return newStudents;	
	}*/
	
	/**
	 * 增加单个学生
	 * @param students  原数组
	 * @param name      增加学生的名字
	 * @param idCard    增加学生的身份证号
	 * @param score     增加学生的分数
	 * @param age       增加学生的年龄
	 * @param sex       增加学生的性别
	 * @return          返回增加后的数组
	 */
	public StudentEntity[] addStudent(StudentEntity[] students,String name,String idCard,double score,int age,String sex){
		
		StudentEntity[] newStudents=new StudentEntity[students.length+1];
		int index=studentExistOrNotByName(students, name);
		if(index>=0){
			System.out.println("该学生已存在，请重新输入");
		}else{
			for(int i=0;i<students.length;i++){
				newStudents[i]=students[i];
			}
			StudentEntity student=new StudentEntity();
			student.setName(name);
			student.setIdCard(idCard);
			student.setAge(age);
			student.setScore(score);
			student.setSex(sex);
			newStudents[students.length]=student;
	
			/*System.out.println(newStudents[students.length]);
			newStudents[students.length].setName(name);
			newStudents[students.length].setIdCard(idCard);
			newStudents[students.length].setScore(score);
			newStudents[students.length].setAge(age);
			newStudents[students.length].setSex(sex);*/
		}
		return newStudents;
		
	}
	/**
	 * 判断一个字符串是否为数字
	 * @param str   被判断的字符串
	 * @return      是，返回true，否，返回false
	 */
	@Test
	public boolean isNumber(String str){
		Pattern pattrern=Pattern.compile("[0-9]*");
		Matcher isNum=pattrern.matcher(str);
		if(!isNum.matches()){
			return false;
		}
		return true;
	}
 

}
